import com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated.JavaLexer;
import com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated.JavaParser;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.TreeAdaptor;

/**
 *
 * @author Padda1
 */
public class FileParser implements LanguageParser
{
    static Vector<Token> list;
    static Vector<Token> colorList;
    /**
    * The default constructor
    */
    FileParser()
    {
        list = new Vector<Token>();
        colorList = new Vector<Token>();
    }

    /**
     * Parses the file who's path is specified by the parameter
     * @param filename String. The URL of the file to parse.
     */
    @Override
    public void parse(String filename) 
    {
        try 
        {
            CharStream c = new ANTLRFileStream(filename);

            Lexer lexer = new JavaLexer(c);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            parser.setTreeAdaptor(adaptor);

            parser.compilationUnit();
            
            Vector<Node> newlist = new Vector<Node>();
            int x = list.size() - 1;

            while(x > 0 )
            {
                Node node = new Node();
                node.setName(list.get(x).getText());
                node.setLineNum(list.get(x).getLine());
                newlist.add(node); 
                x--;
            }

            Process g = new Process(newlist);
            x = 0;
            String tab = "";
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Parses the file who's path is specified by the parameter.
     * @param filename String. The URL of the file to parse.
     */
    @Override
    public Vector<Token> parseColor(String filename) 
    {
        //colorList.clear();
        Vector<Token> colorList1 = new Vector<Token>();
        try 
        {
            CharStream c = new ANTLRFileStream(filename);

            Lexer lexer = new JavaLexer(c);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            parser.setTreeAdaptor(adaptorColor);

            parser.compilationUnit();
            
            int x = 0;
            while(x < colorList.size())
            {
                colorList1.add(colorList.get(x));
                x++;
            }
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
        return colorList1;
    }
    
    /**
     * Parses the string given as the parameter
     * @param filename String. String to parse.
     */
    public void parseString(String fileName) 
    {
        try 
        {
            FileEditor editor = new FileEditor();
            String file = editor.read(fileName);

            CharStream c = new ANTLRStringStream(file.trim());

            Lexer lexer = new JavaLexer(c);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            parser.setTreeAdaptor(adaptor);

            parser.compilationUnit();
            
            Vector<Node> newlist = new Vector<Node>();
            int x = list.size() - 1;

            while(x > 0 )
            {
                Node node = new Node();
                node.setName(list.get(x).getText());
                node.setLineNum(list.get(x).getLine());
                newlist.add(node); 
                x--;
            }
            
            Process g = new Process(newlist);
            x = 0;
            String tab = "";
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
    * The tree returned by the parser.
    */
    static TreeAdaptor adaptor = new CommonTreeAdaptor() 
    {
        public Object create(Token payload) 
        {
            if (payload != null)
            {
                list.add(payload);
            }
            return new CommonTree(payload);
        }
    };
    
    /**
    * The tree returned by the parser.
    */
    static TreeAdaptor adaptorColor = new CommonTreeAdaptor() 
    {
        public Object create(Token payload) 
        {
            if (payload != null)
            {                
                colorList.add(payload);
            }
            return new CommonTree(payload);
        }
    };
    
    /**
    * Returns the parse tree.
    * @return Vector<Token>.
    */
    @Override
    public Vector<Token> getTree() 
    {
        return list;
    }
}
